package com.hckj.jianzhijia.popupwindows;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.hckj.jianzhijia.R;

/**
 * Created by 赵奇隆 on 2015-10-28.
 */
public abstract class ListPopupWindow extends PopupWindow {

    protected Context context;
    protected LayoutInflater layoutInflater;
    protected View anchorView;
    protected View parentView;
    protected String[] data;
    protected ListView listView;


    public ListPopupWindow(Context context, final View parentView, View anchorView, final String[] data) {
        super(context);
        this.context = context;
        this.anchorView = anchorView;
        this.parentView = parentView;
        this.data = data;
        layoutInflater = LayoutInflater.from(context);
        View contentView = layoutInflater.inflate(R.layout.layout_list_popwin, null);
        this.setFocusable(true);
        this.setWindowLayoutMode(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        this.setContentView(contentView);
        this.setOutsideTouchable(true);
        this.setBackgroundDrawable(new BitmapDrawable());
        this.setAnimationStyle(R.anim.abc_popup_enter);
        listView = (ListView) (contentView.findViewById(R.id.lv_select_item));
        listView.setAdapter(new ListPopupWindowAdapter());
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                getListItem(position, data[position]);
                ListPopupWindow.this.dismiss();
            }
        });

        this.setOnDismissListener(new OnDismissListener() {
            @Override
            public void onDismiss() {
                cancelShadow();
            }
        });
        if (anchorView != null) {
            anchorView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (!ListPopupWindow.this.isShowing()) {
                        ListPopupWindow.this.showAtLocation(parentView, Gravity.CENTER, 0, 0);
                    } else {
                        ListPopupWindow.this.dismiss();
                    }
                }
            });
        }
    }


    public abstract void getListItem(int position, String itemName);

    @Override
    public void showAsDropDown(View anchor) {
        super.showAsDropDown(anchor);
        ((Activity) ListPopupWindow.this.context).getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
        WindowManager.LayoutParams params = ((Activity) ListPopupWindow.this.context).getWindow().getAttributes();
        params.alpha = 0.5f;
        ((Activity) ListPopupWindow.this.context).getWindow().setAttributes(params);
    }

    @Override
    public void showAtLocation(View parent, int gravity, int x, int y) {
        super.showAtLocation(parent, gravity, x, y);
        ((Activity) ListPopupWindow.this.context).getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
        WindowManager.LayoutParams params = ((Activity) ListPopupWindow.this.context).getWindow().getAttributes();
        params.alpha = 0.5f;
        ((Activity) ListPopupWindow.this.context).getWindow().setAttributes(params);
    }

    private void cancelShadow() {
        ((Activity) ListPopupWindow.this.context).getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
        WindowManager.LayoutParams params = ((Activity) context).getWindow().getAttributes();
        params.alpha = 1f;
        ((Activity) context).getWindow().setAttributes(params);
    }

    private class ListPopupWindowAdapter extends BaseAdapter {
        @Override
        public int getCount() {
            return data.length;
        }

        @Override
        public Object getItem(int position) {
            return data[position];
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            convertView = layoutInflater.inflate(R.layout.layout_popupwin_item, null);
            TextView tv_item = (TextView) convertView.findViewById(R.id.tv_popwin_item);
            tv_item.setText(data[position]);
            return convertView;
        }
    }
}
